$(function () {
    var user_id = Cookies.get('id');
    if (user_id === undefined) {
        window.location.href = "login.html";
    } else {
        var operator_name = Cookies.get('operator');
        $('.operator span').html(operator_name);
    }

    var option = {
        format: 'YYYY-MM-DD HH:mm:ss'
    };

    $('#datetimepicker1').datetimepicker(option);
    $('#datetimepicker2').datetimepicker(option);

    $('.selectpicker').selectpicker({
        style: 'btn-default',
        size: 7
    });

    $.sidebarMenu($('.sidebar-menu'));
    $('.main-sidebar').height(window.document.body.scrollHeight);

    onElementHeightChange(document.body, function () {
        $('.main-sidebar').height(window.document.body.scrollHeight);
    });
});

function onElementHeightChange(elm, callback) {
    var lastHeight = elm.clientHeight, newHeight;
    (function run() {
        newHeight = elm.clientHeight;
        if (lastHeight !== newHeight)
            callback();
        lastHeight = newHeight;

        if (elm.onElementHeightChangeTimer)
            clearTimeout(elm.onElementHeightChangeTimer);

        elm.onElementHeightChangeTimer = setTimeout(run, 200);
    })();
}

function bytes2Str(arr) {
    var str = "";
    for (var i = 0; i < arr.length; i++) {
        var tmp = arr[i].toString(16);
        if (tmp.length === 1) {
            tmp = "0" + tmp;
        }
        str += tmp;
    }
    return str;
}

function getColumns(table) {
    var columns = [];
    switch (table) {
        case "1": //加密
            var encryption_title = Constant.get_encryption_title();
            var encryption_field = Constant.get_encryption_filed();
            for (var i1 = 0; i1 < encryption_title.length; i1++) {
                if (encryption_field[i1] === "cipherText" || encryption_field[i1] === "plainText" ||
                    encryption_field[i1] === "secretKey") {
                    columns.push({
                        field: encryption_field[i1],
                        title: encryption_title[i1],
                        formatter: formatter_button,
                        events: 'operateEvents'
                    });
                } else {
                    columns.push({
                        field: encryption_field[i1],
                        title: encryption_title[i1]
                    });
                }
            }
            break;
        case "2": //解密
            var decryption_title = Constant.get_decryption_title();
            var decryption_field = Constant.get_decryption_field();
            for (var i2 = 0; i2 < decryption_title.length; i2++) {
                if (decryption_field[i2] === "cipherText" || decryption_field[i2] === "plainText" ||
                    decryption_field[i2] === "secretKey") {
                    columns.push({
                        field: decryption_field[i2],
                        title: decryption_title[i2],
                        formatter: formatter_button,
                        events: 'operateEvents'
                    });
                } else {
                    columns.push({
                        field: decryption_field[i2],
                        title: decryption_title[i2]
                    });
                }
            }
            break;
        case "3"://验证
            var verification_title = Constant.get_verification_title();
            var verification_field = Constant.get_verification_field();
            for (var i3 = 0; i3 < verification_title.length; i3++) {
                if (verification_field[i3] === "publicKey" || verification_field[i3] === "message") {
                    columns.push({
                        field: verification_field[i3],
                        title: verification_title[i3],
                        formatter: formatter_button,
                        events: 'operateEvents'
                    });
                } else {
                    columns.push({
                        field: verification_field[i3],
                        title: verification_title[i3]
                    });
                }
            }
            break;
        case "4"://签名
            var signature_title = Constant.get_signature_title();
            var signature_field = Constant.get_signature_field();
            for (var i4 = 0; i4 < signature_title.length; i4++) {
                if (signature_field[i4] === "privateKey" || signature_field[i4] === "message") {
                    columns.push({
                        field: signature_field[i4],
                        title: signature_title[i4],
                        formatter: formatter_button,
                        events: 'operateEvents'
                    });
                } else {
                    columns.push({
                        field: signature_field[i4],
                        title: signature_title[i4]
                    });
                }
            }
            break;
        case "5"://签密
            var signcryption_title = Constant.get_signcryption_title();
            var signcryption_field = Constant.get_signcryption_field();
            for (var i5 = 0; i5 < signcryption_title.length; i5++) {
                if (signcryption_field[i5] === "encryptKey" || signcryption_field[i5] === "signKey" ||
                    signcryption_field[i5] === "plainText" || signcryption_field[i5] === "cipherText") {
                    columns.push({
                        field: signcryption_field[i5],
                        title: signcryption_title[i5],
                        formatter: formatter_button,
                        events: 'operateEvents'
                    });
                } else {
                    columns.push({
                        field: signcryption_field[i5],
                        title: signcryption_title[i5]
                    });
                }
            }
            break;
        case "6"://解签密
            var unsigncryption_title = Constant.get_unsigncryption_title();
            var unsigncryption_field = Constant.get_unsigncryption_field();
            for (var i6 = 0; i6 < unsigncryption_title.length; i6++) {
                if (unsigncryption_field[i6] === "decryptKey" || unsigncryption_field[i6] === "verifyKey" ||
                    unsigncryption_field[i6] === "plainText" || unsigncryption_field[i6] === "cipherText") {
                    columns.push({
                        field: unsigncryption_field[i6],
                        title: unsigncryption_title[i6],
                        formatter: formatter_button,
                        events: 'operateEvents'
                    });
                } else {
                    columns.push({
                        field: unsigncryption_field[i6],
                        title: unsigncryption_title[i6]
                    });
                }
            }
            break;
        case "7"://MAC
            var mac_title = Constant.get_mac_title();
            var mac_field = Constant.get_mac_field();
            for (var i7 = 0; i7 < mac_title.length; i7++) {
                if (mac_field[i7] === "secretKey" || mac_field[i7] === "message"
                    || mac_field[i7] === "authenticationCode") {
                    columns.push({
                        field: mac_field[i7],
                        title: mac_title[i7],
                        formatter: formatter_button,
                        events: 'operateEvents'
                    });
                } else {
                    columns.push({
                        field: mac_field[i7],
                        title: mac_title[i7]
                    });
                }
            }
            break;
        case "8"://认证加密
            var authenticated_encryption_field = Constant.get_authenticated_encryption_field();
            var authenticated_encryption_title = Constant.get_authenticated_encryption_title();
            for (var i8 = 0; i8 < authenticated_encryption_title.length; i8++) {
                if (authenticated_encryption_field[i8] === "cipherText"
                    || authenticated_encryption_field[i8] === "plainText"
                    || authenticated_encryption_field[i8] === "secretKey") {
                    columns.push({
                        field: authenticated_encryption_field[i8],
                        title: authenticated_encryption_title[i8],
                        formatter: formatter_button,
                        events: 'operateEvents'
                    });
                } else {
                    columns.push({
                        field: authenticated_encryption_field[i8],
                        title: authenticated_encryption_title[i8]
                    });
                }
            }
            break;
        case "9"://解密验证
            var decryption_verification_field = Constant.get_decryption_verification_field();
            var decryption_verification_title = Constant.get_decryption_verification_title();
            for (var i9 = 0; i9 < decryption_verification_title.length; i9++) {
                if (decryption_verification_field[i9] === "cipherText"
                    || decryption_verification_field[i9] === "plainText"
                    || decryption_verification_field[i9] === "secretKey") {
                    columns.push({
                        field: decryption_verification_field[i9],
                        title: decryption_verification_title[i9],
                        formatter: formatter_button,
                        events: 'operateEvents'
                    });
                } else {
                    columns.push({
                        field: decryption_verification_field[i9],
                        title: decryption_verification_title[i9]
                    });
                }
            }
            break;
    }
    return columns;
}

function formatter_button() {
    var check = '<button type="button" class="check btn btn-info">查看</button>';
    var download = '<button type="button" class="download btn btn-info">下载</button>';
    return check + " " + download;
}

function logout() {
    var user_id = Cookies.get("id");
    if (user_id !== undefined) {
        var operator = Cookies.get('operator');
        $.ajax({
            url: 'servlet/LogoutServlet',
            type: 'post',
            data: {
                id: user_id,
                operator: operator
            },
            success: function () {
                window.wxc.xcConfirm("注销成功", window.wxc.xcConfirm.typeEnum.info);
                Cookies.remove("id");
                Cookies.remove("operator");
                window.location.href = "login.html";
            },
            error: function () {
                window.wxc.xcConfirm("注销失败", window.wxc.xcConfirm.typeEnum.error);
            }
        })
    }
}